<?php
namespace lib\rule;
use config\config;
use lib\helper\error;
use lib\redis\RedisTool;
use lib\Tool\Tools;
use lib\rule;


/**
 * Ldy-item-Tools
 * 路由控制及参数处理
 * 实现路由跳转及子集项目分发
 * Class UtilRouter
 * @package lib\rule
 */
class UtilRouter extends ParentInit {

    public static $Vi;
    public static $controller;
    public static $method;
    public static $param;
    public static $Myobject;
    public static $resView;

    public static function Wr(){
        if (empty(self::$Myobject)){
            self::$Myobject = new UtilRouter();
        }
        return  self::$Myobject;
    }

    public function _beginInit()
    {
        parent::__construct();
        config::readRule();
        error::_errorBegin();
        return rule\UtilRouter::Wr()->getParam()->toItem()->toDataFlow();
    }

    /**
     * 参数处理（未完成）
     */
    private function getParam(){
        $param['c'] = !empty($_GET['c'])?$_GET['c']:'default';
        $param['m'] = !empty($_GET['m'])?$_GET['m']:'index';
        self::$param = $param;
        self::$controller =$param['c'];
        self::$method = 'action'.ucfirst($param['m']);
        self::$Vi = 'rpc';
        $this->toCache( @$_SERVER['HTTP_HOST'] . @$_SERVER['REQUEST_URI']);
        return $this;
    }

    /**
     * 进入程序体内
     * @return $this
     */
    private function toItem(){
        $controller = config::get('controllerMap',self::$controller);
        $method = self::$method;
        $controller =  new $controller;
        self::$resView = $controller->$method();
        return $this;
    }

    /**
     * 输出模板渲染
     */
    public function toDataFlow(){

       if (is_array(self::$resView)){
           self::$resView = json_encode(self::$resView,true);
       }
        exit(self::$resView);
    }

    /**
     * 将页面纯静态（将页面缓存纯静态 过期时间一天）
     * @param string $UtilKey
     */
    private function toCache($UtilKey=''){
//        exit();
    }

}